package com.cindata

import com.cindata.dictionary.*
import com.cindata.dimension.RiskDimension
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class WorkflowInstanceStageController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond WorkflowInstanceStage.list(params), model:[workflowInstanceStageCount: WorkflowInstanceStage.count()]
    }

    def show(WorkflowInstanceStage workflowInstanceStage)
    {
        def workflowInstanceConditions = WorkflowInstanceCondition.findAll("from WorkflowInstanceCondition where stage.id = ${workflowInstanceStage?.id} order by executeSequence asc")
        def workflowInstanceEvents = WorkflowInstanceEvent.findAll("from WorkflowInstanceEvent where stage.id = ${workflowInstanceStage?.id} order by executeSequence asc")
        def workflowInstanceUsers = WorkflowInstanceUser.findAllByStage(workflowInstanceStage)
        def workflowInstanceNotifications = WorkflowInstanceNotification.findAllByStage(workflowInstanceStage)
        respond workflowInstanceStage, model: [workflowInstanceConditions: workflowInstanceConditions, workflowInstanceEvents: workflowInstanceEvents, workflowInstanceUsers: workflowInstanceUsers, workflowInstanceNotifications: workflowInstanceNotifications]
    }

    def create() {
        params['targetUri'] = request.getHeader("referer")
        respond new WorkflowInstanceStage(params)
    }

    @Transactional
    def save(WorkflowInstanceStage workflowInstanceStage) {
        workflowInstanceStage.toString()
        if (workflowInstanceStage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (workflowInstanceStage.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond workflowInstanceStage.errors, view:'create'
            return
        }

        workflowInstanceStage.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'workflowInstanceStage.label', default: 'WorkflowInstanceStage'), workflowInstanceStage.id])
                redirect url: params['targetUri']
            }
            '*' { respond workflowInstanceStage, [status: CREATED] }
        }
    }

    def edit(WorkflowInstanceStage workflowInstanceStage) {
        params['targetUri'] = request.getHeader("referer")
        respond workflowInstanceStage
    }

    @Transactional
    def update(WorkflowInstanceStage workflowInstanceStage) {
        if (workflowInstanceStage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (workflowInstanceStage.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond workflowInstanceStage.errors, view:'edit'
            return
        }

        workflowInstanceStage.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'workflowInstanceStage.label', default: 'WorkflowInstanceStage'), workflowInstanceStage.id])
                redirect url: params['targetUri']
            }
            '*'{ respond workflowInstanceStage, [status: OK] }
        }
    }

    @Transactional
    def delete(WorkflowInstanceStage workflowInstanceStage) {

        def targetUri = request.getHeader("referer")
        if (workflowInstanceStage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        workflowInstanceStage.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'workflowInstanceStage.label', default: 'WorkflowInstanceStage'), workflowInstanceStage.id])
                redirect url: targetUri
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'workflowInstanceStage.label', default: 'WorkflowInstanceStage'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
